﻿using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace FastDev
{
    public class LanguageText : MonoBehaviour
    {
        public string ID;
        private Text text;
        private TextMeshProUGUI textMeshPro;

        private void Start()
        {
            text = GetComponent<Text>();
            textMeshPro = GetComponent<TextMeshProUGUI>();

            SetText();
            MsgManager.Instance.Register(MsgID.OnLanguageChange, OnLanguageChange);
        }

        private void OnLanguageChange(object[] obj)
        {
            SetText();
        }

        private void SetText()
        {
            if (text != null)
                text.text = LanguageManager.Instance.GetText(ID);
            else
                textMeshPro.text = LanguageManager.Instance.GetText(ID);
        }

        public string GetText()
        {
            text = GetComponent<Text>();
            textMeshPro = GetComponent<TextMeshProUGUI>();

            return text != null ? text.text : textMeshPro.text;
        }

        [ContextMenu("SetToEnglish")]
        public void SetToEnglish()
        {
            LanguageManager.Instance.SetLanguageType(LanguageType.English);
        }

        [ContextMenu("SetToChinese")]
        public void SetToChinese()
        {
            LanguageManager.Instance.SetLanguageType(LanguageType.Chinese);
        }

    }
}